<?php
require_once(ABSPATH.'include/dao/mydao.php');
require_once(ABSPATH.'include/model/User.php');

class UserDAO{	
	public function save($user){		
		$usr = $this->get($user->id);		
		if(!$usr){
			$this->insert($user);	
		}else{
			$this->update($user);
		}
	}
		
	public function get($id){		
		$sql="select id,access_token,access_token_secret,name,screen_name,profile_image_url,profile,user_sort,active_date,last_update_date,status from users where id='".$id."'";
		$row = mydao_getLine($sql);		
		if(!$row) return false;		
		return $this->convert($row);
	}
	
	public function get_by_name($name){
		$sql="select id,access_token,access_token_secret,name,screen_name,profile_image_url,profile,user_sort,active_date,last_update_date,status from users where name='$name'";
		return $this->convert(mydao_getLine($sql));
	}
	
	#private function
	function insert(&$user) {		
		$this->cc($user);
		$sql="insert into users(id,access_token,access_token_secret,name,screen_name,profile_image_url,profile,user_sort,active_date,last_update_date,status) values(".	
		"'".$user->id."',".
		"'".$user->access_token."',".
		"'".$user->access_token_secret."',".
		"'".$user->name."',".
		"'".$user->screen_name."',".
		"'".$user->profile_image_url."',".
		"'".$user->profile."',".
		"'".$user->user_sort."',".
		"now(),".
		"now(),".
		"'".$user->status."')";	
			
		$user->id = mydao_insert($sql);
	}
	
	function update(&$user){		
		$this->cc($user);
		$sql="update users set ".
		"access_token='".$user->access_token. "',".
		"access_token_secret='".$user->access_token_secret. "',".
		"name='".$user->name. "',".
		"screen_name='".$user->screen_name."',".
		"profile_image_url='".$user->profile_image_url. "',".
		"profile='".$user->profile. "',".
		"user_sort='".$user->user_sort. "',".	
		"last_update_date=now(),".
		"status='".$user->status. "'  ".
		"where id='".$user->id . "'";
		mydao_runSql($sql);		
	}	
	
	function set_status($id,$status){
		$sql="update users set status='".$status."' where id='".$id."'";
		mydao_runSql($sql);
	}
	
	private function convert($result){
		$user = new UserDetails();
		$user->id = $result["id"];
		$user->access_token = $result["access_token"];
		$user->access_token_secret = $result["access_token_secret"];
		$user->name = $result["name"];
		$user->screen_name = $result["screen_name"];
		$user->profile_image_url = $result["profile_image_url"];
		$user->profile = $result["profile"];
		$user->user_sort = $result["user_sort"];
		$user->active_date = $result["active_date"];
		$user->last_update_date = $result["last_update_date"];
		$user->status = $result["status"];
		return $user;
	}
	
	private function cc(&$user){
		$mysql = new SaeMysql();	
		$user->id = $user->id;
		$user->access_token = $mysql-> escape($user->access_token);
		$user->access_token_secret = $mysql-> escape($user->access_token_secret);
		$user->name = $mysql-> escape($user->name);
		$user->screen_name = $mysql-> escape($user->screen_name);
		$user->profile_image_url = $mysql-> escape($user->profile_image_url);
		$user->profile = $mysql-> escape($user->profile);
		#$user->user_sort = $mysql-> escape($user->user_sort);
		#$user->active_date = $mysql-> escape($user->active_date);
		#$user->last_update_date = $mysql-> escape($user->last_update_date);
		#$user->status = $mysql-> escape($user->status);
	}
}
?>